package cn.usain.service.impl;

import cn.jiyun.pojo.Orders;
import cn.jiyun.pojo.vo.OrderResponse;
import cn.usain.mapper.StatisticsMapper;
import cn.usain.service.StatisticsService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    StatisticsMapper statisticsMapper;
//    @Autowired
//    private StatisticsService statisticsService;

/*    @Override
    public double calculateResponseRate() {
        int totalOrders = statisticsMapper.countTotalOrders();
        int respondedOrders = statisticsMapper.countRespondedOrders();

        if (totalOrders == 0) {
            return 0;
        }
        return (double) respondedOrders / totalOrders * 100;
    }

    @Override
    public List<Orders> getOrdersByCondition(String cityLevel, String city, String district, String orderTime, String startTime, String endTime, String orderType) {
        return statisticsMapper.getOrdersByCondition(cityLevel, city, district, orderTime, startTime, endTime, orderType);
    }*/

//
//    @Override
//    public List<Orders> getOrdersByCondition(String cityLevel, String city, String district, String orderTime, String startTime, String endTime, String orderType) {
//        return statisticsMapper.getOrdersByCondition(cityLevel, city, district, orderTime, startTime, endTime, orderType);
//    }


//    @Override
//    public double calculateResponseRate(String startTime, String endTime, String orderType) {
//        List<Orders> allOrders = statisticsMapper.getOrdersForResponseRate(startTime, endTime, orderType);
//        long totalOrders = allOrders.size();
//        long respondedOrders = allOrders.stream()
//                .filter(order -> order.getReceivingTime()!= null)
//                .count();
//        return (double) respondedOrders / totalOrders * 100;
//    }
    @Override
    public OrderResponse getOrderResponse() {
        ArrayList<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        // 获取当天往前11天
        for (int i = -11; i <= 0; i++) {
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR, i);
            Date d = calendar.getTime();
            dateList.add(sdf.format(d));
        }

        // 获取当天往后11天
        for (int i = 1; i <= 11; i++) {
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR, i);
            Date d = calendar.getTime();
            dateList.add(sdf.format(d));
        }


//        for (String s : dateList) {
//
//            OrderResponse orderResponse = new OrderResponse();
//
//            Map<String, Long> map1 = statisticsService.getsuccessNum(s);
//
//            Map<String, Long> map2 = statisticsService.getOrderNum(s);
//
//            if (map2.containsKey(s)) {
//                long value1 = map1.get(s);
//                long value2 = map2.get(s);
//                if (value2!= 0) {
//                    BigDecimal result = BigDecimal.valueOf(value1 / value2);
//                    System.out.println("日期: " + s + " 的运算结果: " + result);
//                } else {
//                    System.out.println("除数不能为0，日期 " + s + " 对应的分母是0");
//                }
//            }
//
//        }


        return null;
    }

    @Override
    public Map<String, Long> getsuccessNum(String s) {
        Map<String, Long> successNumMap = statisticsMapper.getsuccessNum(s);
        return successNumMap;
    }

    @Override
    public Map<String, Long> getOrderNum(String s) {
        Map<String, Long> orderNum = statisticsMapper.getOrderNum(s);
        return orderNum;
    }

    @Override
    public Double getOrderResponseRate() {
        return statisticsMapper.getOrderResponseRate();
    }


}
